package com.crm.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;

import com.crm.entity.Opportunity;
import com.crm.util.ApplicationContext;
import com.crm.util.Constants;

public class AlertsDAO {
	protected final Log logger = LogFactory.getLog(getClass());
	
	private static AlertsDAO singleInstance;
	private AlertsDAO() {}
	
	public static AlertsDAO getSingleInstance() {
	    if (singleInstance == null) {
	      synchronized (AlertsDAO.class) {
	        if (singleInstance == null) {
	          singleInstance = new AlertsDAO();
	        }
	      }
	    }
	    return singleInstance;
	  }
	
	
	/*public AlertsDAO createObject(){
		return new AlertsDAO();
	}*/

	public  List<Object> fiveDaysBefore() throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			Calendar cal = Calendar.getInstance();
		    cal.add(Calendar.DATE, 5);
		    String dt=cal.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH) + 1)+"-"+cal.get(Calendar.DATE);
			String querystr="from Opportunity where id not in(select id from Quotation) and duedate like '%"+dt+"%'";
			Query query=session.createQuery(querystr);
			List<Object> c=query.list();
			return c;		
		}
		catch (Exception ex) {
		     throw ex;
		}
		finally {
		     session.close();
		}
	}
	public  List<Object> threeDaysBefore() throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, 3);
			String dt=cal.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH) + 1)+"-"+cal.get(Calendar.DATE);
			String querystr="from Opportunity where id not in(select id from Quotation) and duedate like '%"+dt+"%'";
			Query query=session.createQuery(querystr);
			List<Object> c=query.list();
			return c;		
		}
		catch (Exception ex) {
		     throw ex;
		}
		finally {
		     session.close();
		}
	}
	public  List<Object> oneDayBefore() throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, 1);
			String dt=cal.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH) + 1)+"-"+cal.get(Calendar.DATE);
			String querystr="from Opportunity where id not in(select id from Quotation) and duedate like '%"+dt+"%'";
			Query query=session.createQuery(querystr);
			List<Object> c=query.list();
			return c;		
		}
		catch (Exception ex) {
		     throw ex;
		}
		finally {
		     session.close();
		}
	}
	public  List<Opportunity> mainAlert(int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			
			List<Opportunity> custObjs=new ArrayList<Opportunity>();
			Date today=new Date();
			int day=1000*60*60*24*5;
			java.sql.Date stdate=new java.sql.Date(today.getTime());
			java.sql.Date enddate=new java.sql.Date(today.getTime()+day);
			
			System.out.println("todays date insql "+new java.sql.Date(today.getTime()+day));
			String querystr="From Opportunity  where business_id=:busid and ((duedate between '"+stdate+"' and '"+enddate+"') and( status!=:status or status!=:closedstatus)) order by duedate";
			Query query=session.createQuery(querystr);
			/*query.setDate("dudt", stdate);
			query.setDate("dudat", enddate);*/
			query.setInteger("busid", busid);
			query.setString("status",Constants.ST_GENERATED_INVOICE );
			query.setString("closedstatus",Constants.ST_CLOSED);
			/*query.setDate("currentDate",);*/
			custObjs=query.list();
			return custObjs;		
		}
		catch (Exception ex) {
		     throw ex;
		}
		finally {
		     session.close();
		}
	}

}
